﻿Public Class EditUser
    Dim MyUser As clsUser
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub EditUser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PopulateUsersDropDown()
    End Sub
    Private Sub PopulateUsersDropDown()
        Me.cbUsers.DisplayMember = "assignedto"
        Me.cbUsers.ValueMember = "username"
        Me.cbUsers.DataSource = clsUIBase.AddUsers()
    End Sub

    Private Sub cbUsers_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbUsers.SelectedValueChanged
        If cbUsers.SelectedIndex > -1 Then
            MyUser = New clsUser(cbUsers.SelectedValue.ToString)
            txtFirstName.Text = MyUser.FirstName.ToString
            txtLastName.Text = MyUser.LastName.ToString
            txtUserEmail.Text = MyUser.UserEmail.ToString
            txtUserID.Text = MyUser.UserID.ToString
            txtUserName.Text = MyUser.UserName.ToString
            If MyUser.IsTech Then
                chkIsTech.CheckState = CheckState.Checked
            Else
                chkIsTech.CheckState = CheckState.Unchecked
            End If
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim istech As Boolean
        If chkIsTech.CheckState = CheckState.Checked Then
            istech = True
        Else
            istech = False
        End If
        MyUser.EditUser(CInt(txtUserID.Text.ToString), txtFirstName.Text.ToString, txtLastName.Text.ToString, txtUserEmail.Text.ToString, txtUserName.Text.ToString, istech)
    End Sub

    Public Function GetRandomPasswordUsingGUID(ByVal length As Integer) As String
        Dim guidResult As String = System.Guid.NewGuid().ToString()
        guidResult = guidResult.Replace("-", String.Empty)
        If length <= 0 OrElse length > guidResult.Length Then
            Throw New ArgumentException("Length must be between 1 and " & guidResult.Length)
        End If
        Return guidResult.Substring(0, length)
    End Function

    Private Sub btnResetPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetPassword.Click
        Dim password As String = GetRandomPasswordUsingGUID(6)
        MyUser.ResetPassword(CInt(txtUserID.Text.ToString), password)
        MsgBox("Password is now set to " & password, vbOKOnly)
    End Sub
End Class